package com.centaline.androidsalesblog.adapter;

import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.ViewGroup;

import com.alexvasilkov.gestures.Settings;
import com.alexvasilkov.gestures.commons.RecyclePagerAdapter;
import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.ImgListBrowseVH;
import com.centanet.framework.glide.GlideLoad;

import java.util.ArrayList;

/**
 * 图片列表大图浏览
 * Created by guilin on 16/1/31.
 */
public class ImgListBrowseAdapter extends RecyclePagerAdapter<ImgListBrowseVH> {

    private final ViewPager viewPager;
    private final DrawableRequestBuilder<String> requestBuilder;
    private final ArrayList<String> list;//大图
    private final ArrayList<String> listThumbnail;//缩略图

    public ImgListBrowseAdapter(ViewPager viewPager, DrawableRequestBuilder<String> requestBuilder,
                                ArrayList<String> list) {
        this(viewPager, requestBuilder, list, null);
    }

    public ImgListBrowseAdapter(ViewPager viewPager, DrawableRequestBuilder<String> requestBuilder,
                                ArrayList<String> list, ArrayList<String> listThumbnail) {
        this.viewPager = viewPager;
        this.requestBuilder = requestBuilder;
        this.list = list;
        this.listThumbnail = listThumbnail;
    }

    @Override
    public ImgListBrowseVH onCreateViewHolder(@NonNull ViewGroup container) {
        ImgListBrowseVH holder = new ImgListBrowseVH(container);
        holder.image.getController().enableScrollInViewPager(viewPager);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ImgListBrowseVH holder, int position) {
        holder.image.getController().getSettings()
                .setPanEnabled(true)
                .setZoomEnabled(true)
                .setDoubleTapEnabled(true)
                .setOverscrollDistance(holder.image.getContext(), 0f, 0f)
                .setOverzoomFactor(2f)
                .setRotationEnabled(false)
                .setRestrictRotation(false)
                .setFillViewport(true)
                .setFitMethod(Settings.Fit.INSIDE)
                .setGravity(Gravity.CENTER);
        GlideLoad.loadBig(new GlideLoad.Builder(requestBuilder, list.get(position))
                .thumbnailUrl(listThumbnail != null && listThumbnail.size() == list.size() ? listThumbnail.get(position) : "")
                .placeHolder(R.drawable.ic_default_est_estate)
                .error(R.drawable.ic_default_est_estate)
                .fitCenter()
                .openWifiSwitch()
                .into(holder.image));
    }

    @Override
    public int getCount() {
        return list.size();
    }
}
